// pages/mine/record/petSick/index.js
const app = getApp()
import {formatDate} from '../../../../utils/util'

Page({
    data: {
        // pet 下拉框
                 // 下拉框选中数据
            choosedPetCondition: {
                nickname: '选择宠物', 
                id: '',
            },
            conditionPetVisible: false,
            conditionPetList: [{
                nickname: '选择所有',
                id:''
            }], // 下拉框数据

        // 日期选择器
            tempDateBegin:formatDate(new Date()),
            tempDateEnd:formatDate(new Date()),
            timeNow: formatDate(new Date()),
    
        //   卡片
            bannerCurrent: 0, // 当前显示的banner, 即petDailyInfo的索引
            notUpdate:true,
         // petSickInfos
            petSickInfos:[{
                id: 0,
                noInfo:'没有符合要求的记录',
            }],
            
        // update 表格数据
            tempFormData:'',
            isCured: 1,
            
        // 添加页面
            show: false,
            duration: 300,
            conditionAddPetVisible:false,
            conditionAddPetList: '',
            choosedAddPetCondition: {
                nickname: '选择宠物', 
                id: -1,
            },
            choosedStartTime: formatDate(new Date()),
            choosedCureTime: formatDate(new Date()),
            addIsCured: 0,
    },

    onLoad(){
        var that = this
        that.initializeDate()
    },

  // 卡片滑动+翻转
  // 滑动
  bannerSwiper(e) {
    const that = this, 
    bannerCurrent = e.detail.current;
    that.setData({
      bannerCurrent,
    })
    console.log("当前bannder: "+bannerCurrent+"\t是否是正面: "+that.data.notUpdate)
  },


    // 搜索
    searchSubmit(e){
        var that = this
        let isCured
        if (e == null || e == '') {
            isCured = that.data.isCured
        } else {
            isCured = e.detail.value.isCured
        }

        let petId = that.data.choosedPetCondition.id
        if (petId == "" || petId == null || petId == 0) {
            petId = ""
        }
        
        // 判空, 不想做

        wx.request({
          url: app.globalData.prefixUrl + '/chong/pet/sick/query',
          data:{
            userId: app.globalData.userId,
            petId,
            isCured,
            // 生病时间 范围
            beginTime: that.data.tempDateBegin,
            endTime:that.data.tempDateEnd
          },
          method:"GET",
          success(e){
              console.log("search的宠物信息:", e)
              if (e.data != null && e.data != '') {
                  // 按照petId, 将pet信息插入到petSickInfos里面
                  let petSickInfos = e.data
                  let petId
                  for (let pet of that.data.conditionPetList) {
                      petId = pet.id
                      for (let petSick of petSickInfos) {
                          if (petId == petSick.petId) {
                              petSick.petNickname = pet.nickname
                              petSick.petPhoto = pet.petPhoto
                          }
                      }
                  }
                that.setData({
                    petSickInfos,
                    notUpdate: true,
                    show: false,
                    bannerCurrent: 0,
                })
              } else {
              that.setData({
                'petSickInfos[0].noInfo': '没有符合要求的记录',
                'petSickInfos[0].id': 0,
                 notUpdate: true,
                 show: false,
                 bannerCurrent: 0,
              })
            }
          }
          
        })
    },
    // 搜索宠物下拉框  改变查询项
    onPetChnageCondition(e) {
        const list = this.data.conditionPetList
        list.forEach(item => {
            if (item.id === e.currentTarget.dataset.id) {
                item.select = true
                this.setData({
                    'choosedPetCondition.nickname': item.nickname,
                    'choosedPetCondition.id': item.id
                })
            } else {
                item.select = false
            }
        })
        if (e.currentTarget.dataset.id == 0) {
            this.setData({
                'choosedPetCondition.nickname': '选择所有宠物',
                'choosedPetCondition.id': '0'
            })
        }
        this.setData({
            conditionPetList: list
        })
    },
    showPetCondition() {
        this.setData({
            conditionPetVisible: !this.data.conditionPetVisible
        })
    },
     // date picker
    changeDateBegin(e){
        var that = this
        console.log("当前Begin选中时间: ", e.detail.value)
        that.setData({ 
          'tempDateBegin':e.detail.value,
      });
      },
    changeDateEnd(e){
        var that = this
        console.log("当前End选中时间: ", e.detail.value)
        that.setData({ 
          'tempDateEnd':e.detail.value,
      });
    },
    // 治愈bindchange
    changeSearchIsCured(e){
        var that = this
        let isCured = e.detail.value
        that.setData({
            isCured
        })
    },


    // 编辑
    updatePetSick(){
        var that  = this
        let tempFormData = that.data.petSickInfos[that.data.bannerCurrent]
        if (tempFormData.isCured == 0) {
            tempFormData.cureTime = that.data.timeNow
        }
        that.setData({
          notUpdate: false,
          tempFormData,
        })
    },
    changeIsCured(e){
        var that = this
        let isCured = e.detail.value
        let tempFormData = that.data.tempFormData
        tempFormData.isCured = isCured
        console.log("isCured: ", isCured)
        that.setData({
            tempFormData
        })
    },
    formChangeDataStart(e){
        var that = this
        console.log("当前编辑选中时间: ", e.detail.value)
        that.setData({ 
          'tempFormData.startTime':e.detail.value,
      });
    },
    formChangeData(e){
        var that = this
        console.log("当前编辑选中时间: ", e.detail.value)
        that.setData({ 
          'tempFormData.cureTime':e.detail.value,
      });
    },
    // 取消编辑
    cancelUpdate(){
        var that = this
        that.setData({
            notUpdate: true,
        })
    },
    // 保存编辑
    saveUpdate(e){
        var that = this
        let tempFormData = that.data.tempFormData
        let formData=e.detail.value;
        let isCured = formData.isCured
        let cureTime = tempFormData.cureTime
        if (isCured == 0) {
           cureTime = ''
        }
        wx.request({
          url: app.globalData.prefixUrl + '/chong/pet/sick/' + tempFormData.id,
          data:{
              id: tempFormData.id,
              type: formData.type,
              startTime: tempFormData.startTime,
              description: formData.description,
              isCured,
              cureTime,
              cureDescription: formData.cureDescription
          },
          method:"PUT",
          header:{
            'Content-type': "application/x-www-form-urlencoded"
          },
          success(e){
              console.log('发送的formData: ', formData)
              console.log('发送的tempFormData: ', tempFormData)
              console.log("record:"+tempFormData.id+"数据更新成功. ", e)
              that.searchSubmit()
          }
        })
    },

    // 删除
      // 删除
    delPetSickInfo(){
    var that = this
     wx.showModal({
        title: '提示',
        content: '确定删除该记录吗?',
        cancelText: '取消',
        confirmText: '确认',
        success: res => {
          if (res.confirm) {
            wx.request({
                url: app.globalData.prefixUrl + '/chong/pet/sick/' + that.data.petSickInfos[that.data.bannerCurrent].id,
                method:"DELETE",
                success(e){
                  console.log("记录 "+that.data.petSickInfos[that.data.bannerCurrent].id+ " 删除成功")
                  let petSickInfos = that.data.petSickInfos
                  petSickInfos.splice(that.data.bannerCurrent, 1)
                  that.setData({
                      petSickInfos
                  })
                  if (petSickInfos.length == 0) {
                    that.setData({
                        'petSickInfos[0].noInfo': '没有符合要求的记录',
                        'petSickInfos[0].id': 0,
                      })
                  }
                },
                fail(e){
                  console.log("记录 "+that.data.petSickInfos[that.data.bannerCurrent].id+ " 删除失败")
                }
              })
          }
        }
      })
    
     },
  


    // 添加
    popAddContainer(){
        // 弹出page-container
        let duration = this.data.duration
        this.setData({
            show: true,
            duration
        })
    },
    onAddPetChnageCondition(e){
        const list = this.data.conditionAddPetList
        list.forEach(item => {
            if (item.id === e.currentTarget.dataset.id) {
                item.select = true
                this.setData({
                    'choosedAddPetCondition.nickname': item.nickname,
                    'choosedAddPetCondition.id': item.id,
                })
            } else {
                item.select = false
            }
        })
        this.setData({
            conditionAddPetList: list
        })
    },
    showAddPetCondition() {
        this.setData({
            conditionAddPetVisible: !this.data.conditionAddPetVisible
        })
    },
    formAddChangeDataStart(e){
        var that = this
        console.log("当前编辑选中时间: ", e.detail.value)
        that.setData({ 
          choosedStartTime: e.detail.value,
      });
    },
    formAddChangeDataCure(e){
        var that = this
        console.log("当前编辑选中时间: ", e.detail.value)
        that.setData({ 
          choosedCureTime: e.detail.value,
      });
    },
    changeAddIsCured(e){
        var that = this
        let addIsCured = e.detail.value
        that.setData({
            addIsCured
        })
    },

      // 保存添加
  saveAdd(e){
    var that = this
    
    let formData=e.detail.value;

    // 判空, 不想做

    wx.request({
      url: app.globalData.prefixUrl +'/chong/pet/sick',
      data:{
        userId: app.globalData.userId,
        petId: that.data.choosedAddPetCondition.id,
        type: formData.type,
        startTime: that.data.choosedStartTime,
        description: formData.description,
        cureDescription: formData.cureDescription,
        isCured: formData.isCured,
        cureTime: formData.isCured == 0? '' : that.data.choosedCureTime
      },
      method:"POST",
      header:{
        'Content-type': "application/x-www-form-urlencoded"
      },
      success(e){
          console.log("添加记录成功: ",e)
          that.searchSubmit()
      },
    })

    that.setData({
        show: false
    })

  },



    initializeDate(){
        var that = this
    
        // 用户宠物数据, 下拉框
        wx.request({
          url: app.globalData.prefixUrl + '/chong/pet/pet/user/' + app.globalData.userId,
          method:"GET",
          success(e){
            console.log("获取当前用户宠物数据成功: ",e)
            that.setData({
                conditionPetList: that.data.conditionPetList.concat(e.data),
                conditionAddPetList: e.data
            })
            that.searchSubmit()
          },
          fail(e){
            console.log("获取当前用户宠物数据失败: ",e)
          }
        })
    },   
    

})